package com.sdhs.projectschedule.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSON;
import com.sdhs.common.core.domain.event.ProcessEvent;
import com.sdhs.common.enums.BusinessStatusEnum;
import com.sdhs.common.utils.DateUtils;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.projectschedule.domain.GcProgressMasterPlan;
import com.sdhs.projectschedule.mapper.GcProgressMasterPlanMapper;
import com.sdhs.projectschedule.mapper.GcProgressMasterPlanMarkMapper;
import com.sdhs.projectschedule.service.IGcProgressMasterPlanMarkService;
import com.sdhs.projectschedule.service.IGcProgressMasterPlanService;

import lombok.extern.slf4j.Slf4j;

/**
 * 项目进度控制--总体计划管理Service业务层处理
 *
 * @author ww
 */
@Service
@Slf4j
public class GcProgressMasterPlanServiceImpl implements IGcProgressMasterPlanService {
	
    @Autowired
    private GcProgressMasterPlanMapper gcProgressMasterPlanMapper;
    
    @Autowired
    private GcProgressMasterPlanMarkMapper gcProgressMasterPlanMarkMapper;
    
    @Autowired
    private IGcProgressMasterPlanMarkService gcProgressMasterPlanMarkService;
    
    @org.springframework.context.event.EventListener(condition = "#processEvent.key.startsWith('overall_plan_formulation')")
    public void processHandler(ProcessEvent processEvent) {
    	// 已通过
    	if (BusinessStatusEnum.FINISH.getStatus().equals(processEvent.getStatus())) {
    		log.info("总体计划制定流程审批通过回调参数：{}", JSON.toJSONString(processEvent));
    		approvedCallback(processEvent.getBusinessKey());
    	}
    }
    
    /**
     * 总体计划制定流程审批通过后，处理预警标识字段的值
     * @param instanceId
     */
    public void approvedCallback(String instanceId) {
    	GcProgressMasterPlan gcProgressMasterPlan = new GcProgressMasterPlan();
    	gcProgressMasterPlan.setInstanceId(instanceId);
    	GcProgressMasterPlan plan = selectGcProgressMasterPlanTreeList(gcProgressMasterPlan);
    	gcProgressMasterPlanMarkService.planMarkWarn(plan.getChildren());
    }

    /**
     * 查询项目进度控制--总体计划管理列表
     *
     * @param gcProgressMasterPlan 项目进度控制--总体计划管理
     * @return 项目进度控制--总体计划管理
     */
    @Override
    public GcProgressMasterPlan selectGcProgressMasterPlanTreeList(GcProgressMasterPlan gcProgressMasterPlan) {
        return gcProgressMasterPlanMapper.selectGcProgressMasterPlanTreeList(gcProgressMasterPlan);
    }

    @Override
    public GcProgressMasterPlan selectGcProgressMasterPlanById(Long id) {
        return gcProgressMasterPlanMapper.selectGcProgressMasterPlanById(id);
    }

    /**
     * 新增项目进度控制--总体计划管理
     *
     * @param gcProgressMasterPlan 项目进度控制--总体计划管理
     * @return 结果
     */
    @Override
    public GcProgressMasterPlan saveOrUpdateGcProgressMasterPlan(GcProgressMasterPlan gcProgressMasterPlan) {
        if (gcProgressMasterPlan.getPlanId() == null) {
            //新增进度计划主表信息
            gcProgressMasterPlan.setApprovalStatus(BusinessStatusEnum.DRAFT.getStatusValue());
            gcProgressMasterPlan.setCreateBy(SecurityUtils.getUsername());
            gcProgressMasterPlan.setCreateTime(DateUtils.getNowDate());
            gcProgressMasterPlanMapper.insertGcProgressMasterPlan(gcProgressMasterPlan);
            gcProgressMasterPlan.setPlanId(gcProgressMasterPlan.getId());
        }
        //新增进度计划子表信息
        gcProgressMasterPlanMarkMapper.deleteGcProgressMasterPlanMarkByPlanId(gcProgressMasterPlan.getPlanId());
        gcProgressMasterPlan.getChildren().forEach(gcProgressMasterPlanMark -> {
            gcProgressMasterPlanMark.setPlanId(gcProgressMasterPlan.getPlanId());
            gcProgressMasterPlanMark.setCreateBy(SecurityUtils.getUsername());
            gcProgressMasterPlanMark.setCreateTime(DateUtils.getNowDate());
        });
        gcProgressMasterPlanMarkMapper.insertGcProgressMasterPlanMarkList(gcProgressMasterPlan.getChildren());
        return gcProgressMasterPlan;
    }
}
